import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { ObjCreate } from "../../common/ObjCreate";
import { ResFileConst } from "../../../resource/ResFileConst";
import { ArenaCrossGroupSelectCellRenderer } from "../cellRenderer/ArenaCrossGroupSelectCellRenderer";
import { LoaderHelp } from "../../../../common/display/LoaderHelp";
import { ArenaCrossGroupNumData } from "../arenaCrossData/ArenaCrossGroupNumData";
import { FrameTimer } from "../../../../../com/gengine/core/frame/FrameTimer";
import { TimerType } from "../../../../../com/gengine/core/frame/TimerType";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../../resource/ImagesConst";
import { GLoadedButton } from "../../../../../com/mui/controls/GLoadedButton";
import { GTileList } from "../../../../../com/mui/controls/GTileList";
import { Vector } from "../../../../../__AS3__/vec/Vector";
//class ArenaCrossGroupSelectPanel
    
    export  class ArenaCrossGroupSelectPanel /* flash.display.Sprite */
    {
        constructor()
        {
            
            this.init();
            this.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddToStage);
            return;
        }

        /* internal  */onAddToStage(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.ArenaCrossGetGroupNumData));
            return;
        }

        /* internal  */init(): void
        {
            var loc1=0;
            var loc2=null;
            ObjCreate.createBg(1, 67, 801, 416, this);
            this._imgTip = new Object /* flash.display.Bitmap */();
            this._imgTip.x = 110;
            this._imgTip.y = 12;
            this.addChild(this._imgTip);
            this._randomBtn = ObjCreate.createGLoadedButton(ResFileConst.ArenaCrossGroupRandomPos, 680, 7, 97, 50, this);
            this._randomBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onClickRandomPos);
            this._vcSelectCellRenderer = new Vector.<ArenaCrossGroupSelectCellRenderer>();
            loc1 = 0;
            while (loc1 < 16) 
            {
                loc2 = new ArenaCrossGroupSelectCellRenderer();
                loc2.x = 5 + 199 * loc1 % 4;
                loc2.y = 73 + 103 * Math.floor(loc1 / 4);
                this.addChild(loc2);
                this._vcSelectCellRenderer.push(loc2);
                ++loc1;
            }
            LoaderHelp.addResCallBack(ResFileConst.ArenaCrossGroupSignUp, this.onResCompl);
            return;
        }

        /* internal  */onClickRandomPos(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._clickRandom = true;
            Dispatcher.dispatchEvent(new DataEvent(EventName.ArenaCrossGroupSelectGroup, 1));
            return;
        }

        /* internal  */onResCompl(): void
        {
            this._isResCompl = true;
            this.updateTipImg();
            return;
        }

        public updateGroupNumData(arg1: Array<any>): void
        {
            if (this._isFlashing) 
            {
                return;
            }
            if (!this._clickRandom || !this._aryGroupData || this._isHaveSelect || !this.isHaveSelect(arg1)) 
            {
                this._aryGroupData = arg1;
                this.drawList();
            }
            else 
            {
                this._clickRandom = false;
                this._aryGroupData = arg1;
                this._isFlashing = true;
                this.randomPosShow(arg1);
            }
            this._isHaveSelect = this.isHaveSelect(arg1);
            this.updateTipImg();
            return;
        }

        /* internal  */isHaveSelect(arg1: Array<any>): Boolean
        {
            if (!arg1 || arg1.length <= 0) 
            {
                return false;
            }
            var loc1=arg1[0] as ArenaCrossGroupNumData;
            return loc1.isHaveSignUp;
        }

        /* internal  */randomPosShow(arg1: Array<any>): void
        {
            var loc2=null;
            this._aryCanSelect = [];
            var loc1=0;
            while (loc1 < arg1.length) 
            {
                loc2 = arg1[loc1] as ArenaCrossGroupNumData;
                if (loc2.currentNum < loc2.maxNum) 
                {
                    this._aryCanSelect.push(loc2);
                }
                ++loc1;
            }
            if (!this._timer) 
            {
                this._timer = new FrameTimer(15);
                this._timer.addListener(TimerType.ENTERFRAME, this.onTimerEnterFrame);
            }
            this._timer.start();
            return;
        }

        /* internal  */onTimerEnterFrame(arg1: FrameTimer): void
        {
            var loc1=this._aryCanSelect[Math.floor(this._aryCanSelect.length * Math.random())] as ArenaCrossGroupNumData;
            var loc2=this._vcSelectCellRenderer[(loc1.groupId - 1)];
            if (this._lastCellRender) 
            {
                this._lastCellRender.randomSelect(false);
            }
            loc2.randomSelect(true);
            this._lastCellRender = loc2;
            if (arg1.currentCount > 10) 
            {
                this.reset();
                this.drawList();
            }
            return;
        }

        /* internal  */reset(): void
        {
            this._timer.stop();
            this._timer.reset();
            this._lastCellRender = null;
            this._isFlashing = false;
            return;
        }

        /* internal  */drawList(): void
        {
            var loc1=0;
            if (this._aryGroupData) 
            {
                loc1 = 0;
                while (loc1 < this._vcSelectCellRenderer.length) 
                {
                    if (loc1 < this._aryGroupData.length) 
                    {
                        this._vcSelectCellRenderer[loc1].data = this._aryGroupData[loc1];
                    }
                    ++loc1;
                }
                this._randomBtn.visible = !this.isHaveSelect(this._aryGroupData);
            }
            return;
        }

        public updateTipImg(): void
        {
            if (this._isResCompl) 
            {
                this._imgTip.bitmapData = GlobalClass.getBitmapData(ImagesConst.ArenaCrossGroupSignUpTimeTip);
                this._imgTip.x = (802 - this._imgTip.width) / 2;
            }
            return;
        }

        /* internal */ /* var */_isLoaded: Boolean=false;

        /* internal */ /* var */_imgTip: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_randomBtn: GLoadedButton;

        /* internal */ /* var */_tileList: GTileList;

        /* internal */ /* var */_vcSelectCellRenderer: Vector.<ArenaCrossGroupSelectCellRenderer>;

        /* internal */ /* var */_tfDes: Object /* flash.text.TextField */;

        /* internal */ /* var */_isResCompl: Boolean=false;

        /* internal */ /* var */_aryGroupData: Array<any>;

        /* internal */ /* var */_aryCanSelect: Array<any>;

        /* internal */ /* var */_isHaveSelect: Boolean=false;

        /* internal */ /* var */_timer: FrameTimer;

        /* internal */ /* var */_lastCellRender: ArenaCrossGroupSelectCellRenderer;

        /* internal */ /* var */_isFlashing: Boolean=false;

        /* internal */ /* var */_clickRandom: Boolean=false;
    }
